package xyz.xtt.asset.client.request;

import java.math.BigDecimal;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.xtt.asset.api.enums.RocketMessageDelayLevelEnum;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class MakeTransactionRequest {
//	private Long assetId;
//	private Long from;
//	private Long to;
//	private Integer num;
//	private String desc;
//	private String orderNo;
//	private String orderType;
//	private String fees;
//	private String price;

	private Long from;
	private Long to;
	@Min(value = 1, message = "最少1个")
	@NotNull(message = "请提供数量")
	private Integer num;
	@Schema(description = "业务方id")
	private Long assetId;
	@Schema(description = "业务方订单号")
	private String orderNo;
	@Schema(description = "单价")
	private String price;
	@Schema(description = "过期分钟数0业务方自己控制")
	private Integer expireTimeWithLevel;
	@Schema(description = "订单类型详见 OrderTypeEnum 枚举")
	private String orderType;
	@Default
	@Schema(description = "支付费用")
	private String fees = "0";
	@Default
	@Schema(description = "订单金额")
	private BigDecimal orderPrice = BigDecimal.ZERO;
	@Default
	@Schema(description = "实际应支付总金额")
	private BigDecimal actTotalPrice = BigDecimal.ZERO;
	@Schema(description = "交易来源")
	private String bizType;
	@Schema(description = "资产来源")
	private String assetSource;
	@Schema(description = "资产编号")
	private Long assetNoId;

	@Schema
	private String remark;

	public boolean withExpire() {
		return Objects.isNull(expireTimeWithLevel) || Objects.equals(expireTimeWithLevel, 0);
	}

	public RocketMessageDelayLevelEnum withRocketMessageDelayLevelEnum() {
		if (withExpire()) {
			return null;
		}
		RocketMessageDelayLevelEnum delayLevel = RocketMessageDelayLevelEnum.convert(expireTimeWithLevel);
		return delayLevel;
	}

	public Boolean checkNum() {
		return Objects.nonNull(num) && num.intValue() > 0;
	}

	public Boolean checkNumWithOne() {
		return Objects.nonNull(num) && num.intValue() == 1;
	}

	public static MakeTransactionRequest getMakeOrder(String platform, TransferFromRequest transferFromRequest, Long assetId) {
		BigDecimal fees = transferFromRequest.getFees();
		String feesVal = Objects.isNull(fees) ? "0" : fees.toPlainString();

		String price = StringUtils.isBlank(transferFromRequest.getPrice()) ? "0" : transferFromRequest.getPrice();
		BigDecimal actTotalPrice = new BigDecimal(String.valueOf(price)).multiply(new BigDecimal(String.valueOf(transferFromRequest.getAmount())));

		return MakeTransactionRequest
				.builder()
				.from(transferFromRequest.getFrom())
				.to(transferFromRequest.getTo())
				.num(transferFromRequest.getAmount())
				.assetId(assetId)
				.orderNo(transferFromRequest.getOrderNo())
				.price(transferFromRequest.getPrice())
				.orderType(transferFromRequest.getOrderType())
				.fees(feesVal)
				.orderPrice(actTotalPrice)
				.actTotalPrice(actTotalPrice.add(new BigDecimal(feesVal)))
				.bizType(platform)
				.remark(transferFromRequest.getRemark())
				.build();

	}
}
